<template>
    <div class="cash flexv">
        <!-- 头部 -->
        <pubHead title="提现"></pubHead>

        <div class="item jus-b ali-c">
            <span>微信账号</span>
            <input type="text" v-model="wxNumber" class="tr" placeholder="请输入微信账号" maxlength="18">
        </div>

        <div class="cash_box mt2 flexv">
            <div class="title">提现金额</div>
            <div class="init_box ali-b">
                <span class="ico">￥</span>
                <input type="tel" maxlength="8" v-model="money" class="flex1" >
            </div>
            <div class="notes jus-b ali-c">
                <div class="ali-c">
                    <span>我的余额{{myMoney.balance}}元，</span>
                    <div class="cashAll" @click="cashAll">全部提现</div>
                </div>

                <span>最低提现额{{myMoney.wx_min_withdraw}}元</span>
            </div>
        </div>

        <div class="cash_btn white tc" @click="cash_btn">提现</div>


    </div>
</template>
<script>
import Vue from 'vue';
import pubHead from '@/components/head';
import { Toast } from 'vant';
import { cash,Withdraw } from '@/request/api.js'
Vue.use(Toast);
export default {
    data(){
        return{
            wxNumber: '', // 微信账号
            money: '', // 提现金额
			myMoney:{},//包含我的余额和最低提现金额
			user:JSON.parse(localStorage.getItem('userInfo'))
        }
    },
    components: {
        pubHead,
    },
    created(){
		this.getData()
    },
    methods: {
        // 获取数据
        getData(){
			this.$loading()
			cash({
				token: localStorage.getItem('token') ? localStorage.getItem('token') : ''
			}).then(res=>{
				Toast.clear()
				this.myMoney = res.data
			})
        },
        // 全部提现
        cashAll(){
            this.money = this.myMoney.balance > 0 ? this.myMoney.balance : '';
        },
        // 提现
        cash_btn(){
            if(!this.wxNumber){
                this.$tip('请输入微信账号!');
                return
            }
            if(!this.money){
                this.$tip('请输入提现金额!');
                return
            }
            if(!this.money){
                this.$tip('请输入提现金额!');
                return
            }
            if(this.money > 0){
                this.money = Number(this.money);
            }
            if(typeof this.money == 'string'){
                this.$tip('请输入正确的金额!');
                return
            }
            if(Number(this.money) < Number(this.myMoney.wx_min_withdraw)){
                this.$tip('最低提现'+ this.myMoney.wx_min_withdraw+'元！');
                return
            }

            if(Number(this.money) > Number(this.myMoney.balance)){
                this.$tip('余额不足!');
                return
            }
			let that = this
			
			Withdraw({
				type:1,
				money:that.money,
				name:that.user.username,
				id:that.user.id,
				token:localStorage.getItem('token')
			}).then(res=>{
				if(res.code==1){
					that.$tip('提交提现申请成功，等待后台审核~')
				}
				else{
					that.$tip(res.msg)
				}
			})
            
        },
    }
}
</script>
<style lang="less" scoped>
.cash{
    min-height: 100vh;
    background-color: #f5f5f5;
    .item{
        height: 1rem;
        padding: 0 0.3rem;
        background-color: #fff;
        font-size: 0.3rem;
        line-height: 120%;
        color: #333;
        input{
            width: 5rem;
            height: 0.6rem;
            margin: auto 0;
            font-size: 0.28rem;
            line-height: 0.6rem;
            color: #333;
        }
    }
    .cash_box{
        background-color: #fff;
        padding: 0.3rem;
        .title{
            font-size: 0.32rem;
            line-height: 120%;
            color: #202020;
        }
        .init_box{
            margin-top: 0.4rem;
            padding-bottom: 0.3rem;
            border-bottom: 1px solid #ededed;
            .ico{
                font-size: 0.88rem;
                line-height: 120%;
                color: #333;
            }
            input{
                width: 2rem;
                height: 0.8rem;
                margin: auto 0;
                font-size: 0.78rem;
                line-height: 0.8rem;
                color: #333;
            }
        }
        .notes{
            height: 0.76rem;
            font-size: 0.24rem;
            line-height: 120%;
            color: #999;
            .cashAll{
                color: #0C5CFF;
            }
        }
    }
    .cash_btn{
        width: 6.5rem;
        height: 0.88rem;
        margin: 2rem auto 0;
        border-radius: 0.44rem;
        font-size: 0.32rem;
        line-height: 0.88rem;
        background-color: #53AB2A;
    }
}
</style>